/*
 * Copyright (c) 2012 Universidad Antonio de Nebrija.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0.html
 * 
 * Contributors:
 *    Universidad Antonio de Nebrija - initial API, implementation and documentation
 */
package com.nebrija.riif.cli;

import java.util.ArrayList;

import com.nebrija.riif.model.RIIFElement;
import com.nebrija.riif.model.RootClass;
import com.nebrija.riif.model.Environment;

/**
 * Command to resolve the cross-references in the property values
 * @author asanchep
 *
 */
public class EnvCommand implements Command {

	@Override
	public String execute(ArrayList<String> params) throws CommandException {
		RootClass r = RootClass.getInstance();
		RIIFElement element = (params.size()>0)?r.getElement(params.get(0)):null;
		if(params.size()>0 && !(element instanceof Environment)){
			return "Error. A valid environment must be referenced";
		}
		if (element!=null){
			r.setActiveEnvironment((Environment)element);
		}
		String envName = (r.getActiveEnvironment()==null)?"undefined":r.getActiveEnvironment().getName();
		return "The current active environment is:" + envName + "\n";
		
	}

	@Override
	public String helpString() throws CommandException {
		return ": set an environment as active";
	}

}
